package xyz.zhzby.cyzx.cms.controller;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.zhzby.cyzx.cms.dto.request.ArticleAddRequest;
import xyz.zhzby.cyzx.cms.dto.request.ArticlePageRequest;
import xyz.zhzby.cyzx.cms.dto.response.ArticleResponse;
import xyz.zhzby.cyzx.cms.service.IArticleService;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.PageResponse;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;

/**
 * @author zby
 * @date 2021/12/10
 */
@RestController
@RequestMapping("/cms/article")
@ApiModel("文章相关API")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @ApiOperation("新增文章/草稿")
    @PostMapping("")
    public RestResponse<Long> createArticle(@RequestBody ArticleAddRequest articleAddRequest) {
        return articleService.createArticle(articleAddRequest);
    }

    @ApiOperation("更新文章/草稿")
    @PutMapping("")
    public RestResponse<Long> updateArticle(@RequestBody ArticleAddRequest articleAddRequest) {
        return articleService.updateArticle(articleAddRequest);
    }

    @ApiOperation("根据ID获取文章信息")
    @GetMapping("/get/{articleID}")
    public RestResponse<ArticleResponse> getArticleInfo(@PathVariable("articleID") Long articleID) {
        return articleService.getArticleById(articleID);
    }

    @ApiOperation("分页获取本人文章")
    @PostMapping("/list")
    public RestResponse<PageResponse<ArticleResponse>> getArticleList(@RequestBody ArticlePageRequest articlePageRequest) {
        articlePageRequest.setUserID(CyzxSecurityContextHolder.getSubject().getId());
        return articleService.getArticleList(articlePageRequest);
    }

    @ApiOperation("获取所有文章")
    @PostMapping("/all")
    public RestResponse<PageResponse<ArticleResponse>> getAllArticles(@RequestBody ArticlePageRequest articlePageRequest) {
        return articleService.getArticleList(articlePageRequest);
    }

    @ApiOperation("根据ID删除文章")
    @DeleteMapping("/{articleID}")
    public RestResponse<String> deleteArticle(@PathVariable("articleID") Long articleID) {
        return articleService.deleteArticle(articleID);
    }

    @ApiOperation("从回收站回收文章至草稿箱")
    @PostMapping("/recycle/{articleID}")
    public RestResponse<String> recycle(@PathVariable("articleID") Long articleID) {
        return articleService.recycle(articleID);
    }

}
